class Product < ActiveRecord::Base
  belongs_to :user
  belongs_to :material
  belongs_to :pcate
  has_many :pcolors,:dependent=>:delete_all
  has_many :line_items,:dependent=>:delete_all
  has_many :prints,:dependent=>:delete_all
  has_and_belongs_to_many :collectors,:class_name=>"User",:join_table => "favorites",:delete_sql =>
    'DELETE FROM favorites WHERE product_id = #{id}'
  has_and_belongs_to_many :reviewer,:class_name=>"User", :join_table => :comments,:delete_sql =>
    'DELETE FROM comments WHERE product_id = #{id}'
  has_many :holds,:dependent=>:delete_all,:order=>"id asc"
  
  attr_accessor :print_array
  attr_accessor :property_hash
  after_save :convert_print_array,:convert_property_hash
  #  has_and_belongs_to_many :orders, :join_table =>:line_items

  GEOMETRY_IMAGE_SIZE = "360"
  MEDIUM_IMAGE_SIZE = "200"
  THUMB_IMAGE_SIZE = "100"


  def mate_price
    price = 0
    price += Material.find(self.material_id).price if self.material_id

    self.prints.each do |d|
      price += d.price
    end

    price
  end

  def designs
    designs =[]
    self.prints.each do |p|
      designs << p.design
    end
    designs
  end

  def designs_string
    return_string = ""
    self.prints.each do |p|
      return_string << "<a href='/designs/#{p.design_id}' rel='#overlay'>#{p.design.sn}</a>
(#{Category.find(p.category_id).name})"
    end
    return_string
  end

  def mate_image_url
    self.material.image_path
  end

  def mate_back_image_url
    self.material.back_image_path
  end

  def check_print_array(prints)
    return false if !prints
    prints.each do |i|
      return false if !Print.exists?(:product_id =>self.id,:design_id=>i[:design_id],
        :category_id=>i[:category_id])
    end
    return true
  end

  def update_quantity(num)
    if self.pcate_id == DIY_CATE
      self.material.quantity +=num
      self.material.save

      self.designs.each do |d|
        d.quantity +=num
        d.save
      end
    else
      self.color.quantity +=num
      self.color.save
    end
  end

  def check_material_design_quantity
    return false if self.material.quantity < 1 || !self.material.active
    self.designs.each do |d|
      return false if d.quantity < 1 || !d.active
    end
    return true
  end

  #  检查是否存在同样的设计
  
  def self.check_exist_by_print(product)
    return false if !product

    products = self.find(:all,:conditions=>["material_id = ?",product[:material_id]])
    return false if !products
    flag = true
    target_id = nil
    target_product = nil

    products.each do |p|
      flag = true
      flag = p. check_print_array(product[:print_array])
      target_id = p.id if flag &&  p.prints.length == product[:print_array].length  #检查图片数量是否相同
    end

    if target_id
      target_product = Product.find(target_id)
    end

    return target_product
  end

  private

  def convert_print_array
    return if !print_array
    Print.delete_all(["product_id = ?",self.id])
    print_array.each do |i|
      Print.create(:product_id =>self.id,
        :category_id=>i[:category_id],
        :design_id=>i[:design_id])
    end
  end

  def convert_property_hash
    return if !property_hash
    Hold.delete_all(["product_id = ?",self.id])
    property_array = []
    self.property_hash.each{|k,v|
      property_array << {:property_id=>k,:value=>v[:value]}
    }
    self.holds.create(property_array)
  end

end
